﻿Option Strict On

Imports System.Collections
Imports System.Collections.Generic
Imports System.Text


Namespace FTP
    ''' <summary>
    ''' Ftp response collection.
    ''' </summary>
    Public Class FtpResponseCollection
        Implements IEnumerable(Of FtpResponse)
        Private _list As New List(Of FtpResponse)()

        ''' <summary>
        ''' Default constructor.
        ''' </summary>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' Searches for the specified object and return the zero-based index of the
        ''' first occurrence within the entire FtpResponseCollection list.
        ''' </summary>
        ''' <param name="item">The FtpResponse object to locate in the collection.</param>
        ''' <return>The zero-based index of the first occurrence of item within the entire if found; otherwise, -1.</return>
        Public Function IndexOf(ByVal item As FtpResponse) As Integer
            Return _list.IndexOf(item)
        End Function



        ''' <summary>
        ''' Adds an FtpResponse to the end of the FtpResponseCollection list.
        ''' </summary>
        ''' <param name="item">FtpResponse object to add.</param>
        Public Sub Add(ByVal item As FtpResponse)
            _list.Add(item)
        End Sub

        ''' <summary>
        '''  Gets the number of elements actually contained in the FtpResponseCollection list.
        ''' </summary>
        Public ReadOnly Property Count() As Integer
            Get
                Return _list.Count
            End Get
        End Property

        Private Function GetEnumerator() As IEnumerator(Of FtpResponse) Implements IEnumerable(Of FtpResponse).GetEnumerator
            Return _list.GetEnumerator()
        End Function

        Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
            Return _list.GetEnumerator()
        End Function

        ''' <summary>
        ''' Gets an FtpResponse from the FtpResponseCollection list based on index value.
        ''' </summary>
        ''' <param name="index">Numeric index of item to retrieve.</param>
        ''' <return>FtpResponse object.</return>
        Default Public ReadOnly Property Item(ByVal index As Integer) As FtpResponse
            Get
                Return _list(index)
            End Get
        End Property

        ''' <summary>
        ''' Remove all elements from the FtpResponseCollection list.
        ''' </summary>
        Public Sub Clear()
            _list.Clear()
        End Sub

        ''' <summary>
        ''' Get the raw FTP server supplied reponse text.
        ''' </summary>
        ''' <return>A string containing the FTP server response.</return>
        Public Function GetRawText() As String
            Dim builder As New StringBuilder()
            For Each item As FtpResponse In _list
                builder.Append(item.RawText)
                builder.Append(vbCr & vbLf)
            Next
            Return builder.ToString()
        End Function

        ''' <summary>
        ''' Get the last server response from the FtpResponseCollection list.
        ''' </summary>
        ''' <return>FtpResponse object.</return>
        Public Function GetLast() As FtpResponse
            If _list.Count = 0 Then
                Return New FtpResponse()
            Else
                Return _list(_list.Count - 1)
            End If
        End Function
    End Class
End Namespace